/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Codec {

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        return serialize(root,"");
    }

    public String serialize(TreeNode root,String data) {
        if(root == null) {
            data += "None,";
        } else {
            data += data.valueOf(root.val) + ","; //valueOf将其转换为字符串
            data = serialize(root.left,data);
            data = serialize(root.right,data);
        }
        return data;
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        String[] dataStr = data.split(",");
        List<String> dataList = new LinkedList<String>(Arrays.asList(dataStr));
        return deserialize(dataList);
    }

    public TreeNode deserialize(List<String> dataList) {
        if(dataList.get(0).equals("None")) {
            dataList.remove(0);
            return null;
        }
        TreeNode root = new TreeNode(Integer.valueOf(dataList.get(0)));
        dataList.remove(0);
        root.left = deserialize(dataList);
        root.right = deserialize(dataList);

        return root;
    }
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));